package ui.activity.mine;

import adapter.WorkingRiderAdapter;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import base.BaseBean;
import base.activity.BaseLayoutActivity;
import bean.WorkRiderBean;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dispatchapp.andlei.R;
import com.mylhyl.acp.Acp;
import com.mylhyl.acp.AcpListener;
import com.mylhyl.acp.AcpOptions;
import netWork.NetUtils;
import utils.GsonUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 骑手列表页面
 */
public class WorkingRiderActivity extends BaseLayoutActivity {
    private RecyclerView recyclerView;
    private WorkingRiderAdapter adapter;
    private List<WorkRiderBean> list = new ArrayList<>();
    private NetUtils netUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("骑手列表",true);
        setTitlefontBg_black();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_workingrider;
    }

    @Override
    protected void findViews() {
        netUtils = new NetUtils(mContext,netRequestCallBack);
        recyclerView = getView(R.id.list_rcy);
    }

    @Override
    protected void formatViews() {
//        for(int i = 0;i<10;i++){
//            TestBean bean = new TestBean();
//            bean.str = i+"";
//            bean.time = "1562397834"+i;
//            list.add(bean);
//        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayout.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new WorkingRiderAdapter(R.layout.item_working_rider,list);
        adapter.bindToRecyclerView(recyclerView);
        recyclerView.setAdapter(adapter);
        adapter.setEmptyView(R.layout.empty_view);
        TextView textView = adapter.getEmptyView().findViewById(R.id.tv_empty);
        textView.setText("暂无骑手信息");
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()){
                    case R.id.tv_phone:
                    case R.id.img_phone:
                        callPhone(list.get(position).getMobile());
                        break;
                }
            }
        });
    }

    @Override
    protected void formatData() {
        /**
         * todo
         * 请求数据
         */
//        Map<String,Object> map = new HashMap<>();
//        map.put("","");
        netUtils.get("rider/user/riders",BaseBean.class,false);
    }

    @Override
    protected void getBundle(Bundle bundle) {

    }

    @Override
    public void onClick(View view) {

    }
    /**
     * 拨打电话（直接拨打电话）
     *
     * @param phoneNumber 电话号码
     */
    @SuppressLint("MissingPermission")
    public void callPhone(final String phoneNumber) {
        if (mActivity == null || android.text.TextUtils.isEmpty(phoneNumber)) {
            return;
        }
        //6.0权限处理
        Acp.getInstance(mActivity).request(new AcpOptions.Builder().setPermissions(
                Manifest.permission.CALL_PHONE).build(), new AcpListener() {
            @SuppressLint("MissingPermission")
            @Override
            public void onGranted() {
                Uri u = Uri.parse("tel:" + phoneNumber);
                Intent it = new Intent(Intent.ACTION_CALL, u);
                mActivity.startActivity(it);
            }

            @Override
            public void onDenied(List<String> permissions) {
            }
        });
    }
    private NetUtils.NetRequestCallBack netRequestCallBack = new NetUtils.NetRequestCallBack() {
        @Override
        public void error(String action, Throwable e, Map tag) {

        }
        @Override
        public void success(String action, BaseBean t, Map tag) {
                if(t.getCode() == 200){
                    list = GsonUtils.getInstance(mActivity).parseNoHeaderJArray(t,WorkRiderBean.class);
                    adapter.setNewData(list);
                }else {
                    toast(t.getMsg());
                }
        }
    };
}
